package com.jc;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.mjoys.util.StringUtil;

public class Config {

	//配置文件的文件名，配置文件位于$BASE/conf下
	private static final String DEFAULT_CONF_FILE = "/baseconfig.properties";

	private static PropertiesConfiguration config = null;
	private static String projectName = null;
	private static String projectRootPath = null;
	private static String projectDomain = null;
	private static String projectPort = null;
	private static String projectUrl = null;
	
	static {
		try {
			String absolutePath = Config.class.getResource(getConfigFile()).getFile();
			config = new PropertiesConfiguration(absolutePath);
			
			PropertiesConfiguration tmpConfig = new PropertiesConfiguration(Config.class.getResource("/system.properties").getFile());
			
			projectName=tmpConfig.getString("name");
			if(projectName!=null){
				projectRootPath=StringUtil.findStr(absolutePath, null, projectName+"/")+projectName;
			}
			
			String domain=tmpConfig.getString("sys.domain");
			projectPort=tmpConfig.getString("port");
			if(domain==null){
				projectDomain=tmpConfig.getString("domain");
				if(StringUtil.isNotEmpty(projectDomain)){
					projectUrl="http://"+projectDomain;
				}
				if(StringUtil.isNotEmpty(projectPort) && !projectPort.equals("80")){
					projectUrl+=":"+projectPort;
				}
			}else{
				projectUrl="http://"+domain;
			}
			
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
	
	public static String getProjectDomain() {
		return projectDomain;
	}
	
	public static String getProjectUrl() {
		return projectUrl;
	}

	public static String getRootPath(){
		return projectRootPath;
	}
	
	public static String getSysPath(String type){
		String tmp=config.getString("sys.path."+type);
		if(tmp!=null){
			return projectRootPath+tmp;
		}
		return null;
	}
	
	protected static String getConfigFile(){
		return DEFAULT_CONF_FILE;
	}

	public static String getString(String key, String defaultValue) {
		return config.getString(key, defaultValue);
	}

	public static String getString(String key) {
		return config.getString(key, null);
	}

	public static int getInt(String key, int defaultValue) {
		return config.getInt(key, defaultValue);
	}

	public static int getInt(String key) {
		return config.getInt(key, 0);
	}
	
	public static long getLong(String key, long defaultValue) {
		return config.getLong(key, defaultValue);
	}

	public static long getLong(String key) {
		return config.getLong(key, 0);
	}

	public static String[] getStringArray(String key) {
		return config.getStringArray(key);
	}

	public static boolean getBoolean(String key, boolean defaultValue) {
		return config.getBoolean(key, defaultValue);
	}

	public static boolean getBoolean(String key) {
		return config.getBoolean(key, false);
	}

	public static float getFloat(String key, float defaultValue) {
		return config.getFloat(key, defaultValue);
	}

	public static float getFloat(String key) {
		return config.getFloat(key, 0);
	}
	
	public static String getProjectPort() {
		return projectPort;
	}
	
}

